﻿using AutoCorrelatorGUI.Model;
using AutoCorrelatorGUI.Properties;
using AutoCorrelatorGUI.ViewModel.Messages;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Win32;

namespace AutoCorrelatorGUI.ViewModel
{
   /// <summary>
   /// This class contains properties that a View can data bind to.
   /// <para>
   /// See http://www.galasoft.ch/mvvm
   /// </para>
   /// </summary>
   public class InputFileViewModel : ViewModelBase
   {
      private IDataService _DataService = null;

      /// <summary>
      /// Initializes a new instance of the MvvmViewModel1 class.
      /// </summary>
      public InputFileViewModel(IDataService dataService)
      {
         _DataService = dataService;

         Messenger.Default.Register<MenuCommandMessage>(this, OnMenuCommand);
      }

      private void OnMenuCommand(MenuCommandMessage command)
      {
         if (command.CommandType == MenuCommandTypes.Open)
         {
            OpenFileDialogCommand.Execute(null);
         }
         else if (command.CommandType == MenuCommandTypes.ProcessInput)
         {
            LoadInputFileCommand.Execute(null);
         }
      }

      /// <summary>
      /// The <see cref="InputFile" /> property's name.
      /// </summary>
      public const string InputFilePropertyName = "InputFile";

      //private string _InputFile = "";

      /// <summary>
      /// Sets and gets the InputFile property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string InputFile
      {
         get
         {
            return Settings.Default.InputFile;
         }

         set
         {
            if (Settings.Default.InputFile == value)
            {
               return;
            }

            Settings.Default.InputFile = value;
            RaisePropertyChanged(InputFilePropertyName);
         }
      }

      /// <summary>
      /// The <see cref="PerformAutoCorrelation" /> property's name.
      /// </summary>
      public const string PerformAutoCorrelationPropertyName = "PerformAutoCorrelation";

      /// <summary>
      /// Sets and gets the PerformAutoCorrelation property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public bool PerformAutoCorrelation
      {
         get
         {
            return Settings.Default.PerformAutoCorrelation;
         }

         set
         {
            if (Settings.Default.PerformAutoCorrelation == value)
            {
               return;
            }

            Settings.Default.PerformAutoCorrelation = value;
            RaisePropertyChanged(PerformAutoCorrelationPropertyName);
         }
      }

      private RelayCommand _OpenFileDialogCommand = null;

      /// <summary>
      /// Gets the OpenFileDialog.
      /// </summary>
      public RelayCommand OpenFileDialogCommand
      {
         get
         {
            return _OpenFileDialogCommand ?? (_OpenFileDialogCommand = new RelayCommand(
                () =>
                {
                   OpenFileDialog ofd = new OpenFileDialog();

                   ofd.InitialDirectory = InputFile;
                   ofd.Multiselect = false;

                   if (!ofd.ShowDialog().GetValueOrDefault(false))
                      return;

                   if (ofd.FileName == null)
                   {
                      ofd.FileName = string.Empty;
                   }

                   InputFile = ofd.FileName;

                }));
         }
      }

      private RelayCommand _LoadInputFileCommand;

      /// <summary>
      /// Gets the LoadInputFile.
      /// </summary>
      public RelayCommand LoadInputFileCommand
      {
         get
         {
            return _LoadInputFileCommand ?? (_LoadInputFileCommand = new RelayCommand(() =>
                {
                   //Send the settings to the service
                   _DataService.SetInputFile(InputFile, PerformAutoCorrelation);
                }));
         }
      }
   }
}
